/*
 * Copyright (c) 2025 zhangxiang (fishlikewater@126.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.fishlikewater.raiden.http.core.uttils;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.github.fishlikewater.raiden.http.core.RequestWrap;
import io.github.fishlikewater.raiden.http.core.constant.HttpConstants;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * {@code CacheManager}
 * 本地缓存管理
 *
 * @author zhangxiang
 * @version 1.1.0
 * @since 2024/12/16
 */
public class CacheManager {

    private final Cache<String, RequestWrap> cache = Caffeine.newBuilder()
            .maximumSize(1_000)
            .expireAfterWrite(HttpConstants.CACHE_TIME_MINUTES, TimeUnit.MINUTES)
            .build();

    public static final CacheManager INSTANCE = new CacheManager();

    public RequestWrap getRequestWrap(String methodName, Supplier<RequestWrap> supplier) {
        RequestWrap requestWrap = this.cache.get(methodName, m -> supplier.get());
        return requestWrap.build();
    }
}
